/*
 * Copyright (c) 2021-2021. Gardel
 *
 * This file is part of Source2Pdf (https://gitee.com/gardel/source2pdf).
 *
 * Source2Pdf is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Source2Pdf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Source2Pdf.  If not, see <http://www.gnu.org/licenses/>.
 */

package cn.sunxinao.source2pdf.model;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Stream;
import javafx.embed.swing.SwingFXUtils;
import javafx.scene.image.Image;
import javafx.scene.image.WritableImage;
import javax.swing.Icon;
import javax.swing.filechooser.FileSystemView;

public class FileNode {
    public static final FileSystemView fileSystemView = FileSystemView.getFileSystemView();
    private final Path path;
    private Image icon;
    private String displayName;

    public FileNode(Path path) {
        this.path = path;
    }

    public FileNode(File file) {
        this(file.toPath());
    }

    public Path getPath() {
        return path;
    }

    public boolean isFile() {
        return Files.isRegularFile(path);
    }

    public boolean isDirectory() {
        return Files.isDirectory(path);
    }

    public boolean exists() {
        return Files.exists(path);
    }

    public boolean notExists() {
        return Files.notExists(path);
    }

    public String getFileName() {
        Path fileName = path.getFileName();
        return Objects.requireNonNullElse(fileName, path).toString();
    }

    public synchronized String getDisplayName() {
        if (displayName == null) {
            displayName = fileSystemView.getSystemDisplayName(path.toFile());
        }
        return displayName;
    }

    public synchronized Image getFileIcon() {
        if (icon == null) {
            Icon ic = fileSystemView.getSystemIcon(path.toFile());
            BufferedImage bufferedImage = new BufferedImage(ic.getIconWidth(), ic.getIconHeight(), BufferedImage.TYPE_4BYTE_ABGR);
            Graphics2D graphics = bufferedImage.createGraphics();
            ic.paintIcon(null, graphics, 0, 0);
            WritableImage image = SwingFXUtils.toFXImage(bufferedImage, null);
            graphics.dispose();
            icon = image;
        }
        return icon;
    }

    public FileNode getParent() {
        Path parent = path.getParent();
        if (parent == null) return null;
        return new FileNode(parent);
    }

    public Stream<FileNode> getChildren() {
        if (isDirectory()) {
            try {
                return Arrays.stream(fileSystemView.getFiles(path.toFile(), true))
                    .map(FileNode::new);
            } catch (Exception e) {
                System.err.println(e.getClass().getName() + ": " + e.getLocalizedMessage());
            }
        }
        return Stream.empty();
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof FileNode fileNode)) return false;
        return Objects.equals(path, fileNode.path);
    }

    @Override
    public int hashCode() {
        return Objects.hash(path);
    }

    @Override
    public String toString() {
        return "FileNode{" +
            "path=" + path +
            ", file=" + isFile() +
            ", directory=" + isDirectory() +
            ", exists=" + exists() +
            ", displayName='" + getDisplayName() + '\'' +
            '}';
    }

}
